package org.smr.dev;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import org.smr.dev.matedata.OrmColumnAndField;

public class Test {

	public static void main(String[] args) {
		VelocityEngine ve = new VelocityEngine();
		ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
		ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

		ve.init();
		Template actionTpt = ve.getTemplate("mapperFileTemplate.vm");
		VelocityContext ctx = new VelocityContext();
		List<OrmColumnAndField> list = new ArrayList<OrmColumnAndField>();
		list.add(new OrmColumnAndField("create_time", "createTime"));
		list.add(new OrmColumnAndField("modify_time", "modifyTime"));
		list.add(new OrmColumnAndField("id", "id"));
		ctx.put("tableName", "t_sys_role");
		ctx.put("MODEL_PROPERTY_VARIABLE", list);
		ctx.put("javaBeanName", "role");
		
		String rootPath = Test.class.getClassLoader().getResource("").getFile();
		System.out.println(rootPath);
		 merge(actionTpt,ctx,"abc.jsp");
	}

	private static void merge(Template template, VelocityContext ctx, String path) {
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(path);
			template.merge(ctx, writer);
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			writer.close();
		}
	}
}
